/**
 * 
 */
package ma.hamada.controller;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import ma.hamada.model.PieceModel;
import ma.hamada.model.PuzzleModel;
import ma.hamada.swing.JFramePuzzleView;
import ma.hamada.swing.JPieceView;

/**
 * @author M.ABDELOUHAB
 * 
 */
public class PuzzleController {

	public JFramePuzzleView puzzleView = null;

	JPieceView jPieceView = null;
	
	JLabel label=null;

	private PuzzleModel model = null;

	public PuzzleController(PuzzleModel model) {
		this.model = model;

		puzzleView = new JFramePuzzleView(this);

		int puzzleXLength = model.getPuzzle().length;

		for (int i = 0; i < model.getPuzzle().length; i++) {
			for (int j = 0; j < model.getPuzzle()[i].length; j++) {

				int puzzleYLength = model.getPuzzle()[i].length;

				PieceModel pieceModel = new PieceModel(String.valueOf(model
						.getPuzzle()[i][j]));
				PieceController pieceController = new PieceController(
						pieceModel,this);
				if (i == puzzleXLength - 1 && j == puzzleYLength - 1) {
					label=new JLabel("");
					label.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
					puzzleView.getContentPane().add(label);
				} else {
					jPieceView = new JPieceView(pieceController,
							pieceModel.getValue());
					puzzleView.getContentPane().add(jPieceView.getButton());
				}
			}
		}

		addListenersToModel();
	}

	private void addListenersToModel() {

	}

	public void displayViews() {
		puzzleView.display();
	}

	public void closeViews() {
		puzzleView.close();
	}

	public void notifyPositionChanged(ActionEvent e) {
		
		JButton button = (JButton) e.getSource();
		
		Dimension size = button.getSize();
		int labelX = label.getX();
        int labelY = label.getY();
        int buttonX = button.getX();
        int buttonY = button.getY();
        int buttonPosX = buttonX / size.width;
        int buttonPosY = buttonY / size.height;
        int buttonIndex = model.getPuzzle()[buttonPosY][buttonPosX];

        if (labelX == buttonX && (labelY - buttonY) == size.height ) {

             int labelIndex = buttonIndex + 4;

             puzzleView.getContentPane().remove(buttonIndex);
             puzzleView.getContentPane().add(label, buttonIndex);
             puzzleView.getContentPane().add(button,labelIndex);
             puzzleView.getContentPane().validate();
        }

        if (labelX == buttonX && (labelY - buttonY) == -size.height ) {

             int labelIndex = buttonIndex - 4;
             puzzleView.getContentPane().remove(labelIndex);
             puzzleView.getContentPane().add(button,labelIndex);
             puzzleView.getContentPane().add(label, buttonIndex);
             puzzleView.getContentPane().validate();
        }

        if (labelY == buttonY && (labelX - buttonX) == size.width ) {

             int labelIndex = buttonIndex + 1;

             puzzleView.getContentPane().remove(buttonIndex);
             puzzleView.getContentPane().add(label, buttonIndex);
             puzzleView.getContentPane().add(button,labelIndex);
             puzzleView.getContentPane().validate();
        }

        if (labelY == buttonY && (labelX - buttonX) == -size.width ) {

             int labelIndex = buttonIndex - 1;

             puzzleView.getContentPane().remove(buttonIndex);
             puzzleView.getContentPane().add(label, labelIndex);
             puzzleView.getContentPane().add(button,labelIndex);
             puzzleView.getContentPane().validate();
        }
	}

}
